﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Text;

namespace MyFrameWork.Dam
{
    public class BaseDAM : BaseIDAM
    {
        private Database _db = null;

        public Database Db
        {
            set
            {
                ExceptionIsConnected();
                _db = value;
            }
        }

        public BaseDAM()
        {
         
        }

        protected void CreateDataBase(String name) 
        {
            ExceptionIsConnected();
            _db = DatabaseFactory.CreateDatabase(name);
        }

        protected void ExceptionIsConnected()
        {
            if (_db != null)
                throw new Exception("PACS.BaseDAM.IsConnected");
        }

        protected virtual void CreateDatabaseByConnectionString(string connectionString)
        {
            throw new Exception("PACS.BaseDAM.ConnectError");
        }

        public DBManager BeginDAMTransaction()
        {
            throw new NotImplementedException();
        }

        public DBManager BeginDAMTransaction(System.Data.IsolationLevel isolationLevel)
        {
            throw new NotImplementedException();
        }

        protected DbCommand GetEmptyCommand() 
        {
            return _db.GetSqlStringCommand("SELECT SYSDATETIME();");
 
        }

        protected DbCommand GetSqlStringCommand(String CommandString)
        {
            return _db.GetSqlStringCommand(CommandString);
        }

        protected DbCommand GetStoredProcCommand(String CommandString)
        {
            return _db.GetStoredProcCommand(CommandString);
        }

        protected void AddInParameter(DbCommand command, String name, DbType dbType)
        {
            _db.AddInParameter(command, name, dbType);
        }

        protected void AddInParameter(DbCommand command, String name, DbType dbType, object value)
        {
            _db.AddInParameter(command, name, dbType, value);
        }

        protected void AddInParameter(DbCommand command, String name, DbType dbType, string sourceColumn, DataRowVersion sourceVersion)
        {
            _db.AddInParameter(command, name, dbType, sourceColumn, sourceVersion);
        }

        protected DataSet ExecuteDataSet(DbCommand command)
        {
            return _db.ExecuteDataSet(command);
        }

        protected DataSet ExecuteDataSet(DbCommand command, DBManager dbManager)
        {
            if (dbManager == null) {
                return _db.ExecuteDataSet(command);
            }

            return _db.ExecuteDataSet(command, dbManager.Transaction);
        }

        protected int ExecuteNonQuery(DbCommand command)
        {
            return _db.ExecuteNonQuery(command);
        }

        protected int ExecuteNonQuery(DbCommand command, DBManager dbManager)
        {
            if (dbManager == null)
            {
                return _db.ExecuteNonQuery(command);
            }

            return _db.ExecuteNonQuery(command, dbManager.Transaction);
        }

        protected object ExecuteScalar(DbCommand command)
        {
            return _db.ExecuteScalar(command);
        }

        protected object ExecuteScalar(DbCommand command, DBManager dbManager)
        {
            if (dbManager == null)
            {
                return _db.ExecuteScalar(command);
            }

            return _db.ExecuteScalar(command, dbManager.Transaction);
        }
    }
}